import { filterOrder, getYuanGong, tradeListFields } from '@/utils/helper';
import { useEffect, useRef, useState } from 'react';
import { ProTable, ProColumns } from '@ant-design/pro-components';
import { countUsdtNumNew, getUsdtNum, isLocal, isSuccess, setDefEmployeeId } from '@/utils';
import { tradeListApi, tradeListNewApi, userListApi } from '@/api';
import dayjs from 'dayjs';
import { createTimeLie, yuanGongLie } from './ComonOrder';

export default function TradeListNew() {
  const [yuanGongs, setYuanGongs] = useState<any>([]);
  const tableRef: any = useRef('');
  const [defYuanGongId, setDefYuanGongId] = useState<any>('');

  useEffect(() => {
    initYuanGong();
  }, []);

  // 请求:成交记录
  async function request(e: any) {
    const params = { ...e, userIds: [e.yuanGongId] };
    if (!params.justMyData) delete params.justMyData;
    console.log('params:成交记录 ', params);
    const res: any = await tradeListNewApi(params);
    console.log('res: 成交记录: ', res.data);
    if (!isSuccess(res)) return { data: [], total: 0 };
    return { data: res?.data.list, total: res?.data?.total, success: true };
  }

  const columns: any[] = [
    {
      title: '合约',
      dataIndex: 'contract',
      hideInSearch: true,
    },
    {
      title: '成交时间',
      dataIndex: 'tstime',
      valueType: 'dateRange',
      hideInTable: true,
      search: { transform: ([startTime, endTime]: any) => ({ startTime, endTime }) },
    },
    yuanGongLie(yuanGongs, defYuanGongId, true),
    {
      title: '成交时间',
      dataIndex: 'createTimeNew',
      hideInSearch: true,
    },
    {
      title: '只看本系统',
      dataIndex: 'justMyData',
      // hideInSearch: !isLocal(),
      valueType: 'switch',
      hideInTable: true,
    },
    {
      title: '方向',
      dataIndex: 'newSize',
      hideInSearch: true,
      render: (text: any, record: any) => (
        <div className={`${text > 0 ? 'color-red' : 'color-green'}`}>
          {text > 0 ? '做多' : '做空'}
        </div>
      ),
    },
    {
      title: '委托ID',
      dataIndex: 'order_id',
      hideInSearch: true,
    },
    {
      title: '数量(USDT)',
      dataIndex: 'newSize',
      hideInSearch: true,
      render: (text: any, record: any) => (
        <div>
          {countUsdtNumNew(record?.price, record?.newSize, record?.contractInfo?.quanto_multiplier)}
        </div>
      ),
    },
    {
      title: '成交均价',
      dataIndex: 'price',
      hideInSearch: true,
    },
    {
      title: '角色',
      dataIndex: 'role',
      hideInSearch: true,
    },
    {
      title: '手续费',
      dataIndex: 'fee',
      render: (text: any) => <div>{text} USDT</div>,
      hideInSearch: true,
    },
  ];

  // 员工列表
  const initYuanGong = async () => {
    const yuanGongs: any = await getYuanGong(userListApi);
    setYuanGongs(yuanGongs);
    setDefYuanGongId(setDefEmployeeId(yuanGongs));
  };

  return (
    <>
      {yuanGongs && Object.keys(yuanGongs).length > 0 && (
        <ProTable
          rowKey="id"
          columns={columns}
          request={request}
          actionRef={tableRef}
          options={false}
          scroll={{ x: 1900 }}
          search={{ defaultCollapsed: false }}
          // pagination={false}
        />
      )}
    </>
  );
}
